"""
文件名: MemberInterface
封装是会员模块的接口
每个接口,至少封装一个方法,如果接口和其他接口有关联,还需要封装用于关联的方法
注册的账号,不要随意删除;记住自己账号id
"""
from utils.send_method import SendMethod
from utils.get_keyword import GetKeyword


class MemberInterface:
    def __init__(self):
        self.url = 'http://47.108.206.100:8085'

    def get_auth_code(self, telephone):
        """
        获取验证码接口
        :return:
        """
        method = 'get'
        url = self.url + '/sso/getAuthCode'
        payload = {'telephone': telephone}
        return SendMethod.send_method(method=method, url=url, params=payload)

    def get_verify_code(self, telephone):
        """
        获取验证码值
        :param: telephone: 电话号码
        :return:
        """
        response = self.get_auth_code(telephone)
        return GetKeyword.get_keyword(response, 'data')

    def register(self, body):
        """
        会员注册接口
        :return:
        """
        method = 'post'  # 请求参数类型x-www-form-urlencoded
        url = self.url + '/sso/register'
        return SendMethod.send_method(method=method, url=url, data=body)

    def login(self, body):
        """
        会员登录接口
        :param username: 用户名
        :param password: 密码
        :return:
        """
        method = 'post'  # 请求参数类型x-www-form-urlencoded
        url = self.url + '/sso/login'
        return SendMethod.send_method(method=method, url=url, data=body)

    def get_token(self, body):
        """
        获取会员token，项目第二天会用
        :return:
        """
        print(body)
        response = self.login(body)
        token = GetKeyword.get_keyword(response, 'token')
        return {'Authorization': f'Bearer {token}'}

    # 查看会员信息(作业)
    def get_user_info(self):
        ...

    # 更新密码(作业)
    def update_pwd(self):
        ...


if __name__ == '__main__':
    url = "http://47.108.206.100:8085"
    member = MemberInterface()
    # telephone = '13123124587'
    # print(member.get_auth_code(telephone))  # 调研获取验证码接口
    # print(member.get_verify_code(telephone))  # 调用获取验证码方法
    # 注册
    # body = {
    #     "authCode": member.get_verify_code(telephone),
    #     "password": "123456",
    #     "telephone": telephone,
    #     "username": "jack"
    # }
    # print(member.register(body))

    # 登录
    body = {
        "username": "jack",
        "password": "123456"
    }
    print(member.login(body))
    # 获取令牌
    # print(member.get_token(body))

